﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "待检任务";
    Layout = null;
}
<link href="~/layui/css/layui.css" rel="stylesheet" />
<script src="~/layui/layui.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>待检任务</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<body>
    <div class="layui-container" style="margin: 15px;">
        <!-- 搜索区域 -->
        <div class="layui-row layui-form">
            <div class="layui-col-md12">
                <div class="layui-inline">
                    <label class="layui-form-label">单据编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="documentsCode" placeholder="请输入单据编码" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">物资名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="materialName" placeholder="请输入物资名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">检测类型</label>
                    <div class="layui-input-inline">
                       <select name="documentsType" lay-filter="documentsType">
                           <option value="">请选择</option>
                           <option value="入库检验">入库检验</option>
                           <option value="出库检验">出库检验</option>
                           <option value="过程检验">过程检验</option>
                       </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search"></i> 搜索
                    </button>
                    <button class="layui-btn layui-btn-primary" lay-submit lay-filter="reset">
                        <i class="layui-icon layui-icon-refresh"></i> 重置
                    </button>
                </div>
            </div>
        </div>
        <!-- 表格 -->
        <table id="interviewTable" lay-filter="interviewTable"></table>
    </div>

    <!-- 操作列模板 -->
    <script type="text/html" id="operationTpl">
        {{# if(d.documentsType !== '出库检验'){ }}
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="invite">
            <i class="layui-icon layui-icon-dialogue"></i>检验
        </a>
        {{# } }}
    </script>

   

    <script>
       

        layui.use(['table', 'form'], function(){
            var table = layui.table,
                form = layui.form,
                layer = layui.layer,
                $ = layui.jquery;

            // 动态计算表格高度
            function calculateTableHeight() {
                var windowHeight = $(window).height();
                var searchAreaHeight = $('.layui-row.layui-form').outerHeight(true) || 0;
                var toolbarHeight = $('.layui-row:eq(1)').outerHeight(true) || 0;
                var paginationHeight = 40; // 分页区域的估计高度
                var padding = 30; // 额外的内边距

                return windowHeight - searchAreaHeight - toolbarHeight - paginationHeight - padding;
            }

            // 监听窗口大小变化
            $(window).resize(function() {
                table.reload('interviewTable', {
                    height: calculateTableHeight()
                });
            });

            // 表格实例
            table.render({
                elem: '#interviewTable',
                url: '/Pendingtasks/GetPendingTasks',
                method: 'get',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                    groups: 5
                },
                request: {
                    pageName: 'page',
                    limitName: 'pageSize'
                },
                parseData: function(res) {
                    console.log("API返回数据:", res); // 调试输出
                    
                    // 处理数据，确保待检数量正确
                    if (res.data && res.data.length > 0) {
                        res.data.forEach(function(item) {
                            // 如果待检数量为0但有合格或不合格数量，则重新计算待检数量
                            if (item.pendingtasksNum === 0 && (item.passesNum > 0 || item.noPassesNum > 0)) {
                                item.pendingtasksNum = item.passesNum + item.noPassesNum;
                                console.log("修正待检数量:", item);
                            }
                        });
                    }
                    
                    return {
                        "code": res.success ? 0 : 1,
                        "msg": res.message || "",
                        "count": res.totalCount,
                        "data": res.data
                    };
                },
                response: {
                    statusName: 'code', // 规定数据状态的字段名称
                    statusCode: 0,      // 规定成功的状态码
                    msgName: 'msg',     // 规定状态信息的字段名称
                    countName: 'count', // 规定数据总数的字段名称
                    dataName: 'data'    // 规定数据列表的字段名称
                },
                cols: [[ // 表头
                    {type: 'checkbox', width: 50},
                    {field: 'documentsCode', title: '单据编码', width: 150},
                    {field: 'documentsType', title: '检测类型', width: 120},
                    {field: 'materialCode', title: '物资编码', width: 120},
                    {field: 'materialName', title: '物资名称', width: 120},
                    {field: 'pendingtasksNum', title: '待检验数量', width: 100},
                    {field: 'passesNum', title: '合格数量', width: 100},
                    {field: 'noPassesNum', title: '不合格数量', width: 100},
                    {title: '操作', width: 120, toolbar: '#operationTpl'}
                ]],
                limits: [5, 8, 10, 12],
                limit: 5,
                height: 'full-180', // 设置表格高度，自动适应
                
            });

            // 监听表格复选框选择
            table.on('checkbox(interviewTable)', function(obj){
                // 获取选中的数据
                var checkStatus = table.checkStatus('interviewTable');
                
            });

            // 监听搜索
            form.on('submit(search)', function(data){
                table.reload('interviewTable', {
                    where: {
                        documentCode: data.field.documentsCode,
                        materialName: data.field.materialName,
                        DocumentsType: data.field.documentsType
                    },
                    page: {
                        curr: 1
                    },
                    done: function() {
                        // 重置导出选中按钮状态
                        $('#exportSelectedBtn').addClass('layui-btn-disabled').prop('disabled', true);
                    }
                });
                return false;
            });

            // 监听重置
            form.on('submit(reset)', function(data){
                // 清空搜索框的值
                document.getElementsByName('documentsCode')[0].value = '';
                document.getElementsByName('materialName')[0].value = '';

                // 重新加载表格，重置到第一页
                table.reload('interviewTable', {
                    where: {
                        documentCode: '',
                        materialName: '',
                        DocumentsType: ''
                    },
                    page: {
                        curr: 1
                    },
                    done: function() {
                        // 重置导出选中按钮状态
                        $('#exportSelectedBtn').addClass('layui-btn-disabled').prop('disabled', true);
                    }
                });

                // 重新渲染表单
                form.render();
                return false;
            });

           

            // 监听工具条事件
            table.on('tool(interviewTable)', function(obj){
                var data = obj.data;
                if(obj.event === 'invite'){
                    // 打开过程检验弹框
                    layer.open({
                        type: 2,
                        title: '过程检验 - ' + data.documentsCode,
                        area: ['90%', '90%'],
                        fixed: false,
                        maxmin: true,
                        content: '/Pendingtasks/Inspect?id=' + data.id,
                        success: function(layero, index) {
                            layer.full(index);
                        }
                    });
                }
            });

            

            
        });
    </script>
</body>
</html>